import pandas as pd
import numpy as np
from IPython.core.display import display
import sys
sys.path.append('/Users/kyohei/code/myPackages')
import stock
import singleFundSummary as sfs
from tqdm import tqdm
import math
import matplotlib
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'AppleGothic'
%matplotlib inline
from IPython.core.display import display
import plotly
from plotly import tools
import plotly.graph_objs as go
import plotly.graph_objects as go
plotly.offline.init_notebook_mode(connected=False)
from pypfopt import expected_returns
from pypfopt import risk_models
from pypfopt.efficient_frontier import EfficientFrontier
from tslearn.utils import to_time_series_dataset
from tslearn.clustering import KShape
def getTmpDf(jp_name=False, year=None, possession=False):
df = pd.read_csv('/Users/kyohei/code/financial/funds.csv', index_col=0)
df.columns = [c.split('_')[-1] for c in df.columns]
if possession == True:
df = df.loc[:,list(possession_funds.keys())]
if year is not None:
df = df.query('"{}-12-31" < date < "{}-01-01"'.format(year-1, year+1))
if jp_name == True:
df.columns = list(map(lambda x: funds[x]['fund_name'], list(df.columns)))
return df
def buyFunds(target_fund, yaer, timing, amount_plan):
"""
IN : TargetFundDataFrame, BuyingTiming, BuyingAmountsList
OUT : df
"""
profit_df = pd.DataFrame(columns=['month', 'amount', 'amount_sum' , 'quantity',
'purchase_price', 'evaluation_price',
'profit', 'actual_profit'])
profit_df['month'] = [i for i in range(1,13)]
profit_df['amount'] = amount_plan
profit_df['amount_sum'] = profit_df['amount'].cumsum()
profit_df = profit_df.set_index('month')
query = '"{yaer}-{:0=2}-{:0=2}" <= date < "{yaer}-{:0=2}-{:0=2}"'.replace('{yaer}', str(yaer))
for i, amount in enumerate(profit_df['amount']):
if amount == 0:
continue
i = i+1
target_range = pd.concat([target_fund.query(query.format(i, timing, i, timing+10)),
target_fund.query(query.format(i+1, 1, i+1, 1+10))])
purchase_price = target_range.iat[0, 0]
profit_df.loc[i, ['purchase_price']] = target_range.iat[0, 0]
profit_df.loc[i, ['quantity']] = math.ceil(profit_df.loc[i, ['amount']]['amount'] /
(profit_df.loc[i, ['purchase_price']]['purchase_price']/10000))
profit_df.loc[i, ['evaluation_price']] = target_fund.query(query.format(i, 1, i, 31)).iat[-1, 0]
profit_df['quantity'] = profit_df['quantity'].cumsum()
profit_df['profit'] = profit_df['quantity'] * (profit_df['evaluation_price']/10000)
profit_df['actual_profit'] = profit_df['quantity'] * (profit_df['evaluation_price']/10000) - profit_df['amount_sum']
return profit_df
funds = stock.getFunds()
fee_df= pd.read_csv('/Users/kyohei/code/financial/fee.csv', index_col=0)
df = pd.read_csv('/Users/kyohei/code/financial/funds.csv', index_col=0)
# <<<<
target_fund_id = 'JP90C000G672'
# >>>>
# sfs.plotEfficientFrontier(df, target_fund_id)
correlation, divergence, similar = sfs.PlotCorrelation(getTmpDf(year=2019), target_fund_id, funds)
基準値遷移
sfs.PlotReferenceValueTransition(getTmpDf(year=2019), target_fund_id, funds, divergence, similar)
bollinger bands width
sfs.PlotBollingerBandsWidth(getTmpDf(year=2019), target_fund_id, funds, period=45)
sfs.PlotBollingerBand(getTmpDf(year=2019), target_fund_id, funds, period=30)
MACD
sfs.PlotMACD(getTmpDf(year=2019), target_fund_id, funds, short_period=9, long_period=26)
AROON
sfs.PlotAroon(getTmpDf(year=2019), target_fund_id, funds, period=25)
chande momentum oscillator
sfs.PlotCMO(getTmpDf(year=2019), target_fund_id, funds, period=14)
detrended price oscillator
sfs.PlotDPO(getTmpDf(year=2019), target_fund_id, funds)
double exponential moving average
sfs.PlotDEMA(getTmpDf(year=2019), target_fund_id, funds, period=14)
double_smoothed_stochastic
sfs.PlotDSS(getTmpDf(year=2019), target_fund_id, funds, period=7)
hull moving average
sfs.PlotHMA(getTmpDf(year=2019), target_fund_id, funds, periods=[20, 25, 50])
price oscillator
sfs.PlotPO(getTmpDf(year=2019), target_fund_id, funds, periods=[7, 14])
relative strength index
sfs.PlotRSI(getTmpDf(year=2019), target_fund_id, funds, period=20)
rate of change
sfs.PlotROC(getTmpDf(year=2019), target_fund_id, funds, period=45)
stochrsi
sfs.PlotStochrsi(getTmpDf(year=2019), target_fund_id, funds, period=30)
vertical horizontal filter
sfs.PlotVHF(getTmpDf(year=2019), target_fund_id, funds, period=28)
volatility
sfs.PlotVolatility(getTmpDf(year=2019), target_fund_id, funds, period=20)